<?php

class RealStream
{
   // public myspaceContext
   
    public $context = NULL;
  
  /**
   * Sets up the Real Time Stream with MySpace Context
   *
   * @param MySpaceContext $context
   * @return object a new real time stream object
   */
    
   public function __construct($context)  {
     $this->context = $context;
   }
   
  /*
   *  Real Time Stream
   *  Resource: http://api.myspace.com/stream/subscription?
   *  Details:  http://wiki.developer.myspace.com/index.php?title=Category:Real_Time_Stream
   *  Description: Add Subscription
   *  Subscription Usage : addSubscription('All','http://myspace.mycomp.com/myposthandler.ashx','{}','',100,100);
   *  @return object of php object representing the url of newly created subscription
   */
  
   public function addSubscription($type, $endPoint, $query, $metaData, $batchSize, $rate, $userAddList=NULL, $userRemoveList=NULL){

	  $REST = 'http://api.myspace.com/stream/subscription?';
	  $method = 'POST';
	  
	  $body = '{"Subscription": {"Type":"'.$type.'",'.
	  							'"Endpoint":"'.$endPoint.'",'.
	  							'"Query":'.$query.','.
	  							'"MetaData":"'.$metaData.'",'.
	  							'"BatchSize":'.$batchSize.','.
	  							'"Rate":'.$rate.','.
	  							'"Format":"application/atom+xml",'.
	  							'"UserList":{ "AddList" : ['.$userAddList.'], "RemoveList" : ['.$userRemoveList.'] } '.
	  							'}}';  		
	  					
	  $response = $this->context->makeOAuthRequest($REST,
								null,
								$method,
								array('Content-Type' => 'application/json'),
								$body
								);
        
	  if($response['status']== 200|201 ){
		  return MySpaceUtils::parseJSON($response['body']);
	  }
  	
    }
    
   /*
   *  Real Time Stream
   *  Resource: http://api.myspace.com/stream/subscription/{subscriptionId}?
   *  Details:  http://wiki.developer.myspace.com/index.php?title=Category:Real_Time_Stream
   *  Description: Updated Subscription
   *  Subscription Usage : updateSubscription('1234', 'All','http://myspace.mycompany.com/handler.ashx','{}','',1,1);
   *  @return object of php object representing the url of newly created subscription
   */
    
   public function updateSubscription($subscriptionId, $type, $endPoint, $query, $metaData, $batchSize, $rate, $userList, $status='Active'){

	  $REST = 'http://api.myspace.com/stream/subscription/'.$subscriptionId.'/?';
	  $method = 'PUT';
	  
	  $body = '{"Subscription": {"Status":"'.$status.'",'.
	  							'"Type":"'.$type.'",'.
	  							'"Endpoint":"'.$endPoint.'",'.
	  							'"Query":'.$query.','.
	  							'"MetaData":"'.$metaData.'",'.
	  							'"BatchSize":'.$batchSize.','.
	  							'"Rate":'.$rate.','.
	  							'"Format":"application/atom+xml",'.
	  							'}}';
	  							
	  $response = $this->context->makeOAuthRequest($REST,
								null,
								$method,
								array('Content-Type' => 'application/json'),
								$body
								);
        
	  if($response['status']== 200|201 ){
		  return MySpaceUtils::parseJSON($response['body']);
	  }
  	
    }
    
    /*
   *  Real Time Stream
   *  Resource: http://api.myspace.com/stream/subscription/{subscriptionId}?
   *  Details:  http://wiki.developer.myspace.com/index.php?title=Category:Real_Time_Stream
   *  Description: Updated Subscription
   *  @param  integer  $subscriptionId  subscription Id
   *  @return object of php object representing the JSON in which the current user subscription
   */  
      
	public function getSubscription($subscriptionId){
      $REST = 'http://api.myspace.com/stream/subscription/'.$subscriptionId.'?';
	  $params = array('format'=>'json');
  	  $responseJSON = $this->context->call($REST, $params, 'GET');
  	  return MySpaceUtils::parseJSON($responseJSON);
    }
    
    /*
   *  Real Time Stream
   *  Resource: http://api.myspace.com/stream/subscription/{subscriptionId}?
   *  Details:  http://wiki.developer.myspace.com/index.php?title=Category:Real_Time_Stream
   *  Description: Delete Subscription
   *  @param  integer  $subscriptionId  subscription Id
   *  @return bool
   */
    
    public function deleteSubscription($subscriptionId) {
  	  $REST = 'http://api.myspace.com/stream/subscription/'.$subscriptionId.'?';
  	  return $this->context->doPut($REST, $params, 'DELETE');
    }
  
   /*
   *  Real Time Stream
   *  Resource: http://api.myspace.com/stream/subscription/@all?
   *  Details:  http://wiki.developer.myspace.com/index.php?title=Category:Real_Time_Stream
   *  Description: Get All Subscription
   *  @param  integer  $subscriptionId  subscription Id
   *  @return object of php object representing the JSON in which the lost of current user subscriptions
   */
    
	public function getAllSubscription(){
      $REST = 'http://api.myspace.com/stream/subscription/@all?';
	  $params = array('format'=>'json');
  	  $responseJSON = $this->context->call($REST, $params, 'GET');
  	  return MySpaceUtils::parseJSON($responseJSON);
    }
    
   /*
   *  Real Time Stream
   *  Resource: http://api.myspace.com/stream/subscription/@all?
   *  Details:  http://wiki.developer.myspace.com/index.php?title=Category:Real_Time_Stream
   *  Description: Delete All Subscription
   *  @param  integer  $subscriptionId  subscription Id
   *  @return bool
   */
    
    public function deleteAllSubscription() {
  	  $REST = 'http://api.myspace.com/stream/subscription/@all?';
  	  return $this->context->doPut($REST, $params, 'DELETE');
    }
}

?>